package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCahceAnnotation;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.dao.GuruDao;
import com.baizhi.cmfz.entity.Guru;
import com.baizhi.cmfz.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GuruServiceImpl implements GuruService {

	@Autowired(required = false)

	private GuruDao guruDao;
	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Map selectGuruPage(int page, int limit) {
		Map map = new HashMap();
		int  offste = (page-1)*limit;
		List<Guru> gurus = guruDao.selectGuruPage(offste, limit);
		int count = guruDao.selectCount();
		map.put("data",gurus);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;
	}

	@Override
	@Transactional
	@DeleteCahceAnnotation
	public Map insertGuru(Guru guru) {
		Map map = new HashMap();
		int i = guruDao.insertGuru(guru);
		if ( i > 0){
			map.put("isInsert","添加成功");
		}else{
			map.put("isInsert","添加失敗");
		}
		return map;
	}

	@Override
	@Transactional
	@DeleteCahceAnnotation
	public Map updateGuru(int guruId,int guruStatus) {
		Map map = new HashMap();
		int i = guruDao.updateGuru(guruId,guruStatus);
		if ( i > 0){
			map.put("isInsert","修改成功");
		}else{
			map.put("isInsert","修改失敗");
		}
		return map;
	}

	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Guru selectById(int guruId) {
		return guruDao.selectById(guruId);
	}


	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public List<Guru> selectAll() {
		return guruDao.selectAll();

	}


	@Override
	@Transactional
	@DeleteCahceAnnotation
	public Map MultiInsert(List<Guru> gurus) {
//		读取文件并将文件中的内容添加到数据库
		Map  map = new HashMap();
		int i = guruDao.MultiInsert(gurus);
		if(i > 0){
			map.put("success","添加成功");
		}else{
			map.put("success","添加失败");
		}
		return map;
	}
}
